import { _decorator, Component, director, EventTouch, find, input, Input, ISchedulable, macro, Node, NodeEventType, Scheduler, UITransform } from 'cc';
import loskc from '../SDK/loskc';
import { losChannel } from '../SDK/losChannel';
const { ccclass, property } = _decorator;

@ccclass('HuaWeiIdleMonitor')
export class HuaWeiIdleMonitor implements ISchedulable {
    private static _inst: HuaWeiIdleMonitor;
    id: string = 'HuaWeiIdleMonitor_234823978sdfasdf5234';
    uuid: string = 'HuaWeiIdleMonitor_34056i3496459684596';
    public static get inst(): HuaWeiIdleMonitor {
        if (this._inst == null) {
            this._inst = new HuaWeiIdleMonitor();
        }
        return this._inst;
    }
    private _huaWeiIdleMonitor: Node = null;
    constructor() {
        this._huaWeiIdleMonitor = new Node();
        let uitransform = this._huaWeiIdleMonitor.addComponent(UITransform);
        uitransform.setContentSize(3000, 3000);
        this._huaWeiIdleMonitor.name = '__huaWeiIdleMonitor__';
        const parent = find('UICanvas');
        this._huaWeiIdleMonitor.layer = parent.layer;
        this._huaWeiIdleMonitor.parent = parent;
        this._huaWeiIdleMonitor.setSiblingIndex(999999999);
        // director.getScene().addChild(huaWeiIdleMonitor);

        director.addPersistRootNode(this._huaWeiIdleMonitor);

        // director.getScheduler().scheduleUpdate(this, 0, false);
    }
    private _cd = 0;
    private _enabledIdleCheck = false;
    private _timer = 0;
    private _idleTime = 999999999999999;
    private _showVideoCount = 0;
    async update(dt: number): Promise<void> {
        this._cd += dt;
        if (this._cd > 3) {
            //保证在最上层
            this._huaWeiIdleMonitor.setSiblingIndex(999999999);
            this._cd = 0;
        }

        if (!this._enabledIdleCheck) {
            return;
        }
        this._timer += dt;
        if (this._timer >= this._idleTime) {
            this._timer = 0;
            loskc.log('检测到用户闲置，弹出视频广告');
            for (let index = 0; index < this._showVideoCount; index++) {
                await losChannel.instance().showVideo({
                    sucCall: () => {

                    },
                    failCall: () => {

                    },
                    endCall: () => {
                        this._timer = 0;
                    }
                });
            }
        }
    }
    public init() {
        losChannel.instance().doAction(28, ['hw'], (enabled, val) => {
            if (enabled) {
                this._huaWeiIdleMonitor.on(NodeEventType.TOUCH_START, (event: EventTouch) => { event.preventSwallow = true }, this);
                this._huaWeiIdleMonitor.on(NodeEventType.TOUCH_MOVE, (event: EventTouch) => { event.preventSwallow = true }, this);
                this._huaWeiIdleMonitor.on(NodeEventType.TOUCH_CANCEL, (event: EventTouch) => { event.preventSwallow = true }, this);
                this._huaWeiIdleMonitor.on(NodeEventType.TOUCH_END, this.onTouchEnd, this);
                director.getScheduler().scheduleUpdate(this, 0, false);
                this.setIdleTime(val);
                this.setShowVideoCount(losChannel.instance().getCtrl('p29'));
            }
        })
    }
    private setIdleTime(time: number) {
        loskc.log(`闲置时间设置为${time}`);
        this._idleTime = time;
        if (this._idleTime > 0) {
            this._enabledIdleCheck = true;
        }
    }
    private setShowVideoCount(count: number) {
        loskc.log(`闲置视频广告播放次数设置为${count}`);
        this._showVideoCount = count;
    }
    private onTouchEnd(event: EventTouch) {
        event.preventSwallow = true;
        loskc.log('重置闲置时间');
        this._timer = 0;
    }
}